<template>
	<view class="container" style="overflow: hidden;">
		<!-- tab栏 -->
		<view class="header f-28 col-3">
			<view @tap.stop="bindHeaderTap" :class="{ active: dataType === 'all' }" data-type="all"><text>全部</text></view>
			<view @tap.stop="bindHeaderTap" :class="{ active: dataType === 'payment' }" data-type="payment"><text>待付款</text></view>
			<view @tap.stop="bindHeaderTap" :class="{ active: dataType === 'delivery' }" data-type="delivery"><text>待发货</text></view>
			<view @tap.stop="bindHeaderTap" :class="{ active: dataType === 'received' }" data-type="received"><text>待收货</text></view>
			<view @tap.stop="bindHeaderTap" :class="{ active: dataType === 'comment' }" data-type="comment"><text>待评价</text></view>
		</view>
		<view class="goods-detail-box">
			<scroll-view @scrolltolower="bindDownLoad" scroll-y style="width: 750rpx;" :style="{ height: scrollHeight + 'px' }">
				<view class="wrapper" wx:if="list.data.length > 0">
					<view class="m-top20" v-for="(item, index) in list.data" :key="index" @tap.stop="navigateToDetail" :data-id="item.order_id">
						<view class="order-box b-f">
							<view class="order-header dis-flex">
								<view class="flex-box t-l"><text class="title">商品列表</text></view>
								<view class="flex-box t-r">
									<text class="cont">{{ item.state_text }}</text>
								</view>
							</view>
						</view>
						<view class="order-box b-f">
							<view class="order-cont dis-flex">
								<view class="order-num">
									<text>订单号：{{ item.order_no }}</text>
								</view>
								<view class="order-time">
									<text>{{ item.create_time }}</text>
								</view>
							</view>
						</view>
						<view class="order-box order-img_list">
							<view class="order-shop">
								<view class="order-list-box dis-flex">
									<view class="left">
										<view class="dis-flex">
											<view class="order-shop-left" v-for="(good, gIndex) in item.goods" :key="gIndex"><image :src="good.image.file_path"></image></view>
										</view>
									</view>
									<view class="right dis-flex flex-x-center flex-y-center">
										<text class="goods-number">共{{ item.goods.length }}件</text>
										<text class="iconfont icon-xiangyoujiantou user-orderJtou"></text>
									</view>
								</view>
							</view>
						</view>
						<view class="order-box b-f">
							<view class="order-btn dis-flex">
								<view class="order-left">
									<view class="text">
										<text>合计：</text>
										<text class="col-m">￥{{ item.pay_price }}</text>
									</view>
								</view>
								<view class="order-right">
									<view class="btn-group dis-flex" v-if="item.order_status.value != 20">
										<!-- 未支付取消订单 -->
										<view v-if="item.pay_status.value == 10"><view @tap.stop="cancelOrder" class="btn-default" :data-id="item.order_id">取消</view></view>
										<!-- 已支付取消订单 -->
										<template v-if="item.order_status.value != 21">
											<view v-if="item.pay_status.value == 20 && item.delivery_status.value == 10"><view @tap.stop="cancelOrder" class="btn-default" :data-id="item.order_id">申请取消</view></view>
											<!-- 订单核销码 -->
											<view v-if="item.pay_status.value == 20 && item.delivery_type.value == DeliveryTypeEnum.EXTRACT.value && item.delivery_status.value == 10">
												<view @tap.stop="onExtractQRCode" class="btn-main" :data-id="item.order_id">
													<text class="iconfont icon-qr-extract"></text>
													<text class="m-l-10">核销码</text>
												</view>
											</view>
										</template>
										<template v-else>
											<view class="f-28 col-8">取消申请中</view>
										</template>
										<!-- 订单付款 -->
										<view v-if="item.pay_status.value == 10"><view @tap.stop="onPayOrder" class="btn-main" :data-id="item.order_id">付款</view></view>
										<!-- 确认收货 -->
										<view v-if="item.delivery_status.value == 20 && item.receipt_status.value == 10"><view @tap.stop="receipt" class="btn-main" :data-id="item.order_id">确认收货</view></view>
										<!-- 订单评价 -->
										<view v-if="item.order_status.value == 30 && item.is_comment == 0"><view @tap.stop="comment" class="btn-default" :data-id="item.order_id">评价</view></view>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
				<view v-if="!isLoading && !list.data.length">
					<view class="yoshop-notcont">
						<text class="iconfont icon-wushuju"></text>
						<text class="cont">亲，您还没有订单信息哦！</text>
					</view>
				</view>
			</scroll-view>
		</view>
		<!-- 支付方式弹窗 -->
		<popup :show="showPayPopup" @close="onTogglePayPopup">
			<view class="pop-orderPay pop-example__container">
				<view class="pop-title"><text class="f-30">请选择支付方式</text></view>
				<view class="pop-content">
					<view class="pay-method">
						<form @submit="onSelectPayType" :data-value="PayTypeEnum.WECHAT.value" report-submit="true" v-if="paymentChannel.WECHAT">
							<button formType="submit" class="btn-normal">
								<view class="pay-item dis-flex flex-x-between">
									<view class="item-left dis-flex flex-y-center">
										<view class="item-left_icon wechat"><text class="iconfont icon-weixinzhifu"></text></view>
										<view class="item-left_text">
											<text>{{ PayTypeEnum.WECHAT.name }}</text>
										</view>
									</view>
								</view>
							</button>
						</form>
						<form @submit="onSelectPayType" :data-value="PayTypeEnum.ALIPAY.value" report-submit="true" v-if="paymentChannel.ALIPAY">
							<button formType="submit" class="btn-normal">
								<view class="pay-item dis-flex flex-x-between">
									<view class="item-left dis-flex flex-y-center">
										<view class="item-left_icon alipay"><text class="iconfont icon-zhifubao"></text></view>
										<view class="item-left_text">
											<text>{{ PayTypeEnum.ALIPAY.name }}</text>
										</view>
									</view>
								</view>
							</button>
						</form>
						<form @submit="onSelectPayType" :data-value="PayTypeEnum.BALANCE.value" report-submit="true" v-if="paymentChannel.BALANCE">
							<button formType="submit" class="btn-normal">
								<view class="pay-item dis-flex flex-x-between">
									<view class="item-left dis-flex flex-y-center">
										<view class="item-left_icon balance"><text class="iconfont icon-qiandai"></text></view>
										<view class="item-left_text">
											<text>{{ PayTypeEnum.BALANCE.name }}</text>
										</view>
									</view>
								</view>
							</button>
						</form>
					</view>
				</view>
			</view>
		</popup>
		<!-- 核销码弹出层 -->
		<popup :show="showQRCodePopup" @close="onToggleQRCodePopup">
			<view class="pop-qrcode pop-example__container">
				<view class="pop-title"><text class="f-30">核销二维码</text></view>
				<view class="pop-content"><image :src="QRCodeImage"></image></view>
			</view>
		</popup>
	</view>
</template>

<script>
import Tools from '@/utils/Tools.js';
import Route from '@/utils/Route.js';
import popup from '@/components/popup/index.vue';
// 枚举类：发货方式
import DeliveryTypeEnum from '@/utils/enum/DeliveryType.js';

// 枚举类：支付方式
import PayTypeEnum from '@/utils/enum/order/PayType';

import Payment from '@/utils/Payment.js';
export default {
	components: {
		popup
	},
	data() {
		return {
			dataType: 'all',
			scrollHeight: null,
			list: { data: [] },
			DeliveryTypeEnum, // 配送方式
			PayTypeEnum, // 支付方式

			no_more: false, // 没有更多数据
			isLoading: true, // 是否正在加载中

			page: 1, // 当前页码

			showQRCodePopup: false, // 核销码弹窗显示隐藏
			QRCodeImage: '', // 核销码图片，
			showPayPopup: false,
			paymentChannel: {},
			authId:null
		};
	},
	onLoad(options) {
		this.dataType = options.type || 'all';
		this.authId = uni.getStorageSync('authId') || null;
		this.setListHeight();
		const paymentFilter = Payment.filter();
		this.paymentChannel = paymentFilter.paymentChannel;
	},
	onShow() {
		this.getOrderList();
	},
	methods: {
		/**
		 * 切换标签
		 */
		bindHeaderTap(e) {
			this.dataType = e.currentTarget.dataset.type;
			this.list = { list: { data: [] } };
			this.isLoading = true;
			this.page = 1;
			this.no_more = false;
			// 获取订单列表
			this.$nextTick(() => {
				this.getOrderList(e.currentTarget.dataset.type);
			});
		},
		/**
		 * 设置商品列表高度
		 */
		setListHeight() {
			let systemInfo = uni.getSystemInfoSync();
			let scrollHeight = systemInfo.windowHeight - uni.upx2px(88); // swiper高度
			this.scrollHeight = scrollHeight;
		},
		/**
		 * 获取订单列表
		 */
		getOrderList(isPage, page) {
			this.$get(
				this.$api.getUserOrderLists,
				{
					page: page || 1,
					dataType: this.dataType
				},
				res => {
					let resList = res.data.list;
					let dataList = this.list;
					if (isPage == true) {
						this.$set(this.list, 'data', dataList.data.concat(resList.data));
						this.isLoading = false;
					} else {
						this.list = resList;
						this.isLoading = false;
					}
				}
			);
		},
		/**
		 * 下拉到底加载数据
		 */
		bindDownLoad() {
			// 已经是最后一页
			if (this.page >= this.list.last_page) {
				this.no_more = true;

				return false;
			}
			// 加载下一页列表
			this.getOrderList(true, ++this.page);
		},
		/**
		 * 取消订单
		 */
		cancelOrder(e) {
			let order_id = e.currentTarget.dataset.id;
			uni.showModal({
				title: '友情提示',
				content: '确认要取消该订单吗？',
				success: o => {
					if (o.confirm) {
						this.$post(this.$api.cancelUserOrder, { order_id }, res => {
							this.getOrderList(this.dataType);
						});
					}
				}
			});
		},
		/**
		 * 点击付款按钮
		 */
		onPayOrder(e) {
			// 记录订单id
			this.payOrderId = e.currentTarget.dataset.id;

			// 显示支付方式弹窗
			this.onTogglePayPopup();
		},
		/**
		 * 选择支付方式
		 */
		onSelectPayType(e) {
			// 记录formId
			this.$saveFormId(e.detail.formId);
			// 隐藏支付方式弹窗
			this.onTogglePayPopup();
			if (!this.showPayPopup) {
				// 发起付款请求
				// console.log(e.currentTarget.dataset.value)
				this.payment(this.payOrderId, e.currentTarget.dataset.value);
			}
		},
		/**
		 * 显示/隐藏支付方式弹窗
		 */
		onTogglePayPopup() {
			this.showPayPopup = !this.showPayPopup;
		},
		/**
		 * 发起付款请求
		 */
		payment(orderId, payType) {
			// 显示loading
			wx.showLoading({
				title: '正在处理...'
			});
			const postData = {
				order_id: orderId,
				payType: payType,
				return_url: Tools.getSite() + '/pages/order/index'
			};
			if (this.authId) {
				this.$set(postData, 'id', this.authId);
			}
			this.$post(
				this.$api.payUserOrder,
				postData,
				res => {
					if (res.code === -10) {
						Tools.showError(result.msg);
						return false;
					}
					// // 发起微信支付
					// if (res.data.pay_type == PayTypeEnum.WECHAT.value) {
					// 	// App.wxPayment({
					// 	// 	payment: result.data.payment,
					// 	// 	success() {
					// 	// 		// 跳转到已付款订单
					// 	// 		wx.navigateTo({
					// 	// 			url: '../order/detail?order_id=' + orderId
					// 	// 		});
					// 	// 	},
					// 	// 	fail() {
					// 	// 		App.showError(result.msg.error);
					// 	// 	}
					// 	// });
					// }
					// 余额支付
					if (res.data.pay_type == PayTypeEnum.BALANCE.value) {
						Tools.showSuccess(res.msg.success, () => {
							// 跳转到已付款订单
							Route.navigateTo({
								url: '../order/detail?order_id=' + orderId
							});
						});
					} else {
						Payment.pay(
							res.data.payment,
							res.data.pay_type,
							() => {
								this.getOrderList(this.dataType);
							},
							msg => {
							}
						);
					}
				},
				null,
				() => {
					uni.hideLoading();
				}
			);
		},
		/**
		 * 确认收货
		 */
		receipt(e) {
			let order_id = e.currentTarget.dataset.id;
			wx.showModal({
				title: '提示',
				content: '确认收到商品？',
				success: o => {
					if (o.confirm) {
						this.$post(
							this.$api.receiptUserOrder,
							{
								order_id
							},
							result => {
								this.getOrderList(this.dataType);
							}
						);
					}
				}
			});
		},
		/**
		 * 跳转订单详情页
		 */
		navigateToDetail(e) {
			let order_id = e.currentTarget.dataset.id;
			Route.navigateTo({
				url: '../order/detail?order_id=' + order_id
			});
		},
		/**
		 * 查看核销二维码
		 */
		onExtractQRCode(e) {
			let order_id = e.currentTarget.dataset.id;
			// 调用后台api获取核销二维码
			uni.showLoading({
				title: '加载中'
			});
			this.$get(
				this.$api.getUserOrderExtractQrcode,
				{ order_id },
				res => {
					this.QRCodeImage = res.data.qrcode;
					// 显示核销二维码
					this.onToggleQRCodePopup();
				},
				null,
				() => {
					wx.hideLoading();
				}
			);
		},
		/**
		 * 核销码弹出层
		 */
		onToggleQRCodePopup() {
			this.showQRCodePopup = !this.showQRCodePopup;
		},
		/**
		 * 订单评价
		 */
		comment(e) {
			let order_id = e.currentTarget.dataset.id;
			uni.navigateTo({
				url: './comment/comment?order_id=' + order_id
			});
		}
	}
};
</script>

<style>
.header {
	width: 100%;
	height: 88rpx;
	border-bottom: 1rpx solid #e4e4e4;
	display: flex;
	background: #fff;
	line-height: 88rpx;
	position: sticky;
	/* top: var(--window-top); */
	z-index: 3;
}

.header view {
	flex: 1;
	text-align: center;
	padding: 0 15rpx;
}

.header view text {
	display: block;
}

.header view.active {
	color: #fd4a5f;
}

.header view.active text {
	border-bottom: 1px solid #fd4a5f;
}

.order-box {
	padding: 0 15px;
}

.order-header {
	/* border-bottom: 1rpx solid #e4e4e4; */
	padding-top: 10rpx;
}

.order-header .title {
	font-size: 30rpx;
	color: #333;
}

.order-header .cont {
	/* display: block; */
	font-size: 30rpx;
	color: #fd4a5f;
}

.order-cont {
	padding: 15rpx 0;
	justify-content: space-between;
}

.order-cont .order-num text,
.order-cont .order-time text {
	color: #777;
	font-size: 26rpx;
}

.order-shop {
	padding: 16rpx 0 16rpx 0;
}

.order-shop-left,
.order-shop-left image {
	width: 160rpx;
	height: 160rpx;
	/* border: 1rpx solid #e4e4e4; */
	margin-right: 15rpx;
	display: inline-block;
	background: #fff;
}

.order-shop .num {
	float: right;
}

.order-btn {
	padding: 18rpx 0 18rpx 0;
}

.order-btn .order-left {
	flex: 3;
}

.order-btn .order-left text,
.order-btn .order-left .text {
	font-size: 29rpx;
}

.order-btn .order-left .text {
	color: #888;
}

.order-btn .order-right {
	flex: 3;
}

.order-btn .order-right .btn-group {
	justify-content: flex-end;
}

.order-list-box .left {
	flex: 4;
	overflow: hidden;
	position: relative;
}

.order-list-box .right {
	flex: 1;
}

.goods-number {
	font-size: 28rpx;
	color: #777;
}

.btn-default {
	border-radius: 4px;
	border: 1rpx solid #ccc;
	padding: 6rpx 20rpx;
	font-size: 28rpx;
	color: #555;
	float: right;
}

.btn-main {
	border-radius: 4px;
	border: 1rpx solid #fd4a5f;
	padding: 6rpx 20rpx;
	font-size: 28rpx;
	color: #fd4a5f;
	margin-left: 10rpx;
	float: right;
}

.user-orderJtou {
	font-size: 24rpx;
}

.order-img_list {
	background: #fff;
	border-top: 1rpx solid #f1f1f1;
	border-bottom: 1rpx solid #f1f1f1;
}

.order-right-name {
	position: absolute;
	top: 50%;
	left: 180rpx;
	font-size: 30rpx;
	color: #555;
	width: 100%;
	margin-top: -46rpx;
	line-height: 1.6;
}

/* 弹出层 */

.pop-example__container {
	background: #fff;
	padding: 30rpx 40rpx;
	border-radius: 10rpx;
	min-width: 460rpx;
}

.pop-example__container .pop-title {
	text-align: center;
}

.pop-content {
	margin-top: 30rpx;
}

/* 弹出层：核销码 */

.pop-qrcode .pop-content image {
	display: block;
	width: 460rpx;
	height: 460rpx;
}

/* 弹出层：支付方式 */

.pop-orderPay .pop-title {
	text-align: left;
}

.pay-method .pay-item {
	padding: 20rpx 0;
	font-size: 28rpx;
	/* border-bottom: 1rpx solid rgb(248, 248, 248); */
}

.pay-method .pay-item .item-left_icon {
	margin-right: 20rpx;
	font-size: 32rpx;
}

.pay-method .pay-item .item-left_icon.wechat {
	color: #00c800;
}

.pay-method .pay-item .item-left_icon.balance {
	color: #ff9700;
}
.pay-method .pay-item .item-left_icon.alipay {
	color: #0093e2;
}
</style>
